package yf.mall.service.impl.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yf.response.RRException;
import com.yf.utils.HttpTookit;
import com.yf.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import yf.mall.dysdk.model.request.text.DyTextRequest;
import yf.mall.dysdk.model.request.video.*;
import yf.mall.entity.AppConfigEntity;
import yf.mall.service.app.AppConfigService;
import yf.mall.service.app.DyUtilService;
import yf.mall.utils.StringUtil;
import yf.mall.vo.app.dy.DyVideoVO;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class DyUtilServiceImpl implements DyUtilService {

    public static List<DyVideoBaseRequest> dyRequests;


    public DyVideoBaseRequest dyRequest(Integer type) {
        if (dyRequests == null) {
            DyVideoRequest0 request0 = new DyVideoRequest0();
            DyVideoRequest1 request1 = new DyVideoRequest1();
            DyVideoRequest2 request2 = new DyVideoRequest2();
            DyVideoRequest3 request3 = new DyVideoRequest3();
//            DyVideoRequest4 request4 = new DyVideoRequest4();
            dyRequests = new ArrayList<>();
            dyRequests.add(request0);
            dyRequests.add(request1);
            dyRequests.add(request2);
            dyRequests.add(request3);
//            dyRequests.add(request4);
        }
        if (type > dyRequests.size()) {
           type = type%(dyRequests.size()+1);
        }
        return dyRequests.get(type);
    }

    @Resource
    private AppConfigService appConfigService;





    @Override
    public DyVideoVO parseVideo(String link,Integer type) {
        //主力
        AppConfigEntity appConfigEntity = appConfigService.findConfig("DY_REQUEST_TYPE");
        //备份
        AppConfigEntity appBfConfigEntity = appConfigService.findConfig("DY_REQUEST_BF_TYPE");
        if(type==null){ //不存在type
            if (appConfigEntity != null) {
                type = Integer.parseInt(appConfigEntity.getValue());
            }else {
                type = 0;
            }
        }
        DyVideoVO dyVideoVO;
        try {
            dyVideoVO = dyRequest(type).parseVideo(link);
            //不存在且是第三方的地址
            if(dyVideoVO.getVideoUrl()!=null&&dyVideoVO.getVideoUrl().contains("default.365yg")){ //包含的话 换一下
                dyVideoVO.setVideoUrl(dyVideoVO.getVideoUrl().replace("default.365yg","cold.机构vod"));
                log.info("default.365yg换成cold.douyinvod:最后:{}",dyVideoVO.getVideoUrl());
            }
//            int count = 1;
//            while (!StringUtil.isBlank(dyVideoVO.getVideoUrl())){
//                log.info("抖音安全下载链接循环获取,{},{}",count,dyVideoVO.getVideoUrl());
//                count++;
//                dyVideoVO = dyRequest(type).parseVideo(link);
//                if(count>3){ //循环3次了 最多三次
//                    break;
//                }
//            }
        } catch (Exception e) {
            if(appBfConfigEntity!=null){
                type = Integer.valueOf(appBfConfigEntity.getValue());
            }else { //随机一个
                type = new Random().nextInt(4);
            }
            log.error("抖音去水印接口失败,切换到type:{}",type);
            dyVideoVO = dyRequest(type).parseVideo(link);
        }
        return dyVideoVO;
    }

    @Override
    public Map parseText(String text) {
        if(text==null||text.equals("")){
            throw new RRException("传入参数");
        }
        DyTextRequest textRequest = new DyTextRequest();
        String res = textRequest.parse(text);
        System.out.println(res);
        JSONObject jsonObject = JSONObject.parseObject(res);
        if(jsonObject.getIntValue("StateCode")!=1){
            throw new RRException("不存在");
        }
        TreeMap map = new TreeMap();
        JSONArray array = jsonObject.getJSONArray("Result");
        for (int i = 0; i < array.size(); i++) {
            JSONObject tem = array.getJSONObject(i);
            List<String> strs = new ArrayList<>();
            JSONArray items = tem.getJSONArray("item");
            for (int j = 0; j < items.size(); j++) {
                strs.add(items.getJSONObject(j).getString("word"));
            }
            map.put(tem.getString("levelName"),strs);
        }
        return map;
    }
}
